package com.maxatime.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.exception.PhoenixException;
import com.maxatime.pojo.*;
import com.maxatime.service.GradeService;
import com.maxatime.utils.MyContent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author Mr.Zhao
 * @title: GradeController
 * @projectName phoenix
 * @description: TODO
 * @date 2021/7/29 21:24
 */
@Controller
public class GradeController {
    Logger log = LogManager.getLogger();
    @Autowired
    private GradeService gradeService;

    @RequestMapping("/gradeFind")
    @ResponseBody
    public Map<String, Object> gradeFind(@RequestParam(required = false, defaultValue = "1") int page,
                                         @RequestParam(required = false, defaultValue = "7") int limit,
                                         GradeVo vo

    ) {
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<Classroom> stu = gradeService.gradeFind(vo);// 这是我们的sql
//        for(StudentMessage i:stu){
//            i.setNativePlace(i.getNativePlace());
//        }

//        log.info("data_______:::::"+stu);
//        log.info("String studentName, String mobile, String sex,Integer age,String idNum：：：：：：："
//                +studentName+"   "
//                +mobile+"   "
//                +sex+"   "
//                +age+"   "
//                +idNum+"   "
//        );
        // 使用pageInfo包装查询
        PageInfo<Classroom> rolePageInfo = new PageInfo<>(stu);//
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", rolePageInfo.getTotal());
        map.put("data", rolePageInfo.getList());
        log.info("map------------------------------------" + map);
        return map;

    }

    @RequestMapping("/grade")
    public String grade() {
        return "/html/gradelist";
    }

    @RequestMapping("/gradeDelete")
    @ResponseBody
    public Integer gradeDelete(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String pk = jsonObject.getString("pkId");
        int pkId = Integer.parseInt(pk);


        return gradeService.gradeDelete(pkId);
    }

    @RequestMapping("/EmployeeInfo")
    @ResponseBody
    public List<EmployeeInfo> EmployeeInfoList() {

        return gradeService.EmployeeInfoList();
    }

    @RequestMapping("/classroomList")
    @ResponseBody
    public List<Classroom> classroomList() {

        return gradeService.classroomList();
    }


    @ResponseBody
    @RequestMapping("/updateGrade")
    public Map<String, Object> updateGrade(@RequestBody Grade grade) {
        Map<String, Object> m = new HashMap<String, Object>();

        EmployeeInfo em = gradeService.selectKe(grade.getEmployeeId());
        Classroom cla = gradeService.selectList(grade.getClassroomId());

        grade.setLecturerName(em.getEmployeeName());
        grade.setClassTeacherName(em.getEmployeeName());
        grade.setClassroomClassroomNumber(cla.getClassroomNumber());

        Calendar now = Calendar.getInstance();//获取时间
        grade.setUpdateTime(now.getTime());//当前时间

        try {
            Integer cc = gradeService.updateGrade(grade);
            m.put(MyContent.CODE, "0000");
            m.put(MyContent.MSG, "修改成功");
            m.put("data", cc);


        } catch (PhoenixException e) {
            e.printStackTrace();
            log.error("exception::" + e.getMessage());
            log.error("exception--code" + e.getCode());
            log.error("exception--msg" + e.getMsg());
            m.put(MyContent.CODE, e.getCode());
            m.put(MyContent.MSG, e.getMsg());
            m.put("data", 0);
        }

        return m;
    }

    @RequestMapping("/selectKey/{Id}")
    public String selectKey(@PathVariable Integer Id, HttpSession session) {

        Grade grade = gradeService.selectKey(Id);
        session.setAttribute("grade", grade);


        return "html/gradestu";


    }

    @RequestMapping("/insertGrade")
    @ResponseBody
    public Map<String, Object> insertGrade(@RequestBody Grade grade) {
        Map<String, Object> m = new HashMap<String, Object>();

//          log.info("ffffffffffffff"+grade.getLecturerId());
//        log.info("ffffffffffffff"+grade.getClassTeacherId());
//
//        EmployeeInfo em = gradeService.selectKe(grade.getLecturerId());
//        Classroom cla = gradeService.selectList(grade.getClassroomClassroomId());
//
//        //log.info("wrgryh::"+em.getEmployeeName());
//        grade.setLecturerName(em.getEmployeeName());
//        grade.setClassTeacherName(em.getEmployeeName());
//        grade.setClassroomClassroomNumber(cla.getClassroomNumber());
        GradeClassroomRelation gcr = new GradeClassroomRelation();
        gcr.setGradeGradeId(grade.getGradeId());
        gcr.setClassroomClassroomId(grade.getClassroomClassroomId());
        gcr.setDeleteFlag("0");


        Calendar now = Calendar.getInstance();//获取时间
        grade.setUpdateTime(now.getTime());
        grade.setCreateTime(now.getTime());//当前时间

        gcr.setCreateTime(now.getTime());
        gcr.setUpdateTime(now.getTime());
        grade.setDeleteFlag("0");

        try {
            Integer dd = gradeService.addgrc(gcr);
            Integer cc = gradeService.addGrade(grade);
            m.put(MyContent.CODE, "0000");
            m.put(MyContent.MSG, "添加成功");
            m.put("data", cc);


        } catch (PhoenixException e) {
            e.printStackTrace();
            log.error("exception::" + e.getMessage());
            log.error("exception--code" + e.getCode());
            log.error("exception--msg" + e.getMsg());
            m.put(MyContent.CODE, e.getCode());
            m.put(MyContent.MSG, e.getMsg());
            m.put("data", 0);
        }

        return m;


    }

    @RequestMapping("/deleteListGrade")
    @ResponseBody
    public Integer deleteListGrade(@RequestBody String jsonStr) {

        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        JSONArray pkId = jsonObject.getJSONArray("pkId");
        Integer[] pkid = new Integer[pkId.size()];
        for (int i = 0; i < pkId.size(); i++) {
            Object id = pkId.get(i);
            pkid[i] = Integer.parseInt(id.toString());
            log.info("id————————————" + Integer.parseInt(id.toString()));
        }
        List<Integer> list = Arrays.asList(pkid);

        return gradeService.deleteListGrade(list);


    }
    @RequestMapping("/viewstudent/{Id}")
    public String viewstudent(@PathVariable Integer Id, HttpSession session) {

                  session.setAttribute("dd",Id);

        return "html/viewgrade";


    }
    @RequestMapping("/viewstudentin")
    @ResponseBody
    public Map<String,Object> viewstudentin(@RequestParam(required=false,defaultValue="1") int page,
                                          @RequestParam(required=false,defaultValue="7") int limit,
                                            String gradeId
                                         ){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<StudentMessage> stu  = gradeService.viewstudentin(gradeId);// 这是我们的sql

        for(StudentMessage i:stu){
            i.setNativePlace(i.getNativePlace());
        }

        // 使用pageInfo包装查询
        PageInfo<StudentMessage> rolePageInfo = new PageInfo<>(stu);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }

    @RequestMapping("/selectbanjistu/{gradeId}")
    public String viewstu(@PathVariable Integer gradeId, HttpSession session) {

        session.setAttribute("pp",gradeId);

        return "html/selectbanjistu";


    }







}

